package com.xinkao.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.xinkao.example.config.annotation.SysLog;
import com.xinkao.example.service.DictService;
import com.xinkao.example.util.CommonUtil;
import com.xinkao.example.util.RedisUtils;
import com.xinkao.example.util.constants.Constants;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhoujing
 * @date 2021/3/11 9:15
 * @description 字典相关controller
 */
@RestController
@RequestMapping("/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 保存
     */
    @SysLog(module = "字典模块", operate = "保存")
    @RequiresPermissions("dict:save")
    @PostMapping("/save")
    public JSONObject saveDict(HttpServletRequest request, @RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "name,dictKey");
        String token = request.getHeader(Constants.AUTH);
        JSONObject user = redisUtils.get(token, JSONObject.class);
        return dictService.saveDict(user,requestJson);
    }

    /**
     * 列表
     * @param requestJson
     * @return
     */
    @SysLog(module = "字典模块", operate = "分页列表")
    @RequiresPermissions("dict:list")
    @PostMapping("/list")
    public JSONObject listDict(@RequestBody JSONObject requestJson){
        CommonUtil.hasAllRequired(requestJson, "pageNum,pageRow");
        return dictService.listDict(requestJson);
    }

    /**
     * 删除
     * @return
     */
    @SysLog(module = "字典模块", operate = "删除")
    @RequiresPermissions("dict:delete")
    @PostMapping("/delete")
    public JSONObject deleteDict(@RequestBody JSONObject jsonObject){
        CommonUtil.hasAllRequired(jsonObject, "id");
        return dictService.deleteDict(jsonObject);
    }

    /**
     * 字典类别列表
     * @return
     */
    @SysLog(module = "字典模块", operate = "字典类别列表")
    @GetMapping("/listDictKey")
    public JSONObject listDictKey(){
        return dictService.listDictKey();
    }
}
